#! coding: utf-8

import os
import json
import traceback

from addon_manager.addon_config import AddOnConfig
from addon_manager.addon_logger import AddOnLogger


class AddOnWhitelist(object):
    """
    AddonWhiteList用于为特定的AddOn指定上传，下载，启动，停止，端口固定等权限
    """

    __ADDON_WHITELIST_INST = None

    __PROP_KEY_DISPLAY = "display"
    __PROP_KEY_UPDATE = "update"
    __PROP_KEY_PORT = "port"
    __PROP_KEY_DELETE = "delete"
    __PROP_KEY_EXPORT = "export"

    @staticmethod
    def inst():
        if AddOnWhitelist.__ADDON_WHITELIST_INST is None:
            AddOnWhitelist.__ADDON_WHITELIST_INST = AddOnWhitelist()
        return AddOnWhitelist.__ADDON_WHITELIST_INST

    def __init__(self):
        self.config = AddOnConfig.inst()
        self.whitelist = {}
        self._load_whitelist(self.config.whitelist_file)

    def _load_whitelist(self, whitelist_file):
        """
        加载.whitelist, 根据whitelist获取内置AddOn权限信息
        """
        if not os.path.exists(whitelist_file):
            return

        try:
            with open(whitelist_file, "r") as fp:
                whitelist = json.load(fp)
        except Exception:
            AddOnLogger().error(traceback.format_exc())
        else:
            self.whitelist = whitelist

    def get_fixed_port(self, addon_name):
        """
        根据addon_name获取固定端口
        """
        addon_ctrl_info = self.whitelist.get(addon_name)
        if addon_ctrl_info is None:
            return None
        return addon_ctrl_info.get(self.__PROP_KEY_PORT)

    def get_addon_name_port_pairs(self):
        """
        获取所有builtin-addon中的名称和固定端口
        """
        pair_list = []
        for addon_name in self.whitelist:
            port = self.get_fixed_port(addon_name)
            if port is not None:
                pair_list.append([addon_name, port])
        return pair_list

    def is_builtin_addon(self, addon_name):
        """
        判断是否是系统内置的AddOn
        """
        return self.whitelist.get(addon_name) is not None

    def is_display_disable(self, addon_name):
        """
        判断是否可以被显示在AddOnList中. 默认为显示
        """
        addon_ctrl_info = self.whitelist.get(addon_name)
        if addon_ctrl_info is None:
            return False
        return addon_ctrl_info.get(self.__PROP_KEY_DISPLAY) == False

    def is_update_disable(self, addon_name):
        """
        判断AddOn是否可以重新安装, 默认可以重新安装
        """
        addon_ctrl_info = self.whitelist.get(addon_name)
        if addon_ctrl_info is None:
            return False
        return (addon_ctrl_info.get(self.__PROP_KEY_DISPLAY) == False) or addon_ctrl_info.get(self.__PROP_KEY_UPDATE) == False

    def is_delete_disable(self, addon_name):
        """
        判断AddOn是否可以被删除, 默认可以删除
        """
        addon_ctrl_info = self.whitelist.get(addon_name)
        if addon_ctrl_info is None:
            return False
        return (addon_ctrl_info.get(self.__PROP_KEY_DISPLAY) == False) or (addon_ctrl_info.get(self.__PROP_KEY_DELETE) == False)

    def is_export_disable(self, addon_name):
        addon_ctrl_info = self.whitelist.get(addon_name)
        if addon_ctrl_info is None:
            return False

        return (addon_ctrl_info.get(self.__PROP_KEY_DISPLAY) == False) or (addon_ctrl_info.get(self.__PROP_KEY_EXPORT) == False)


__all__ = ["AddOnWhitelist"]
